package com.kehutong;

import java.io.IOException;
import javax.servlet.ServletContextEvent;

import com.kehutong.charge.job.ChargeBillHouseJob;
import org.coraframework.authz.AuthzRealm;
import org.coraframework.boot.Application;
import org.coraframework.boot.Binding;
import org.coraframework.boot.BootApplication;
import org.coraframework.boot.annotation.Initialize;
import org.coraframework.boot.db.DBInit;
import org.coraframework.inject.Binder;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.jdbc.connection.DataBaseConfig;
import org.coraframework.orm.jdbc.connection.IDBConfig;
import org.coraframework.util.Ioc;
import com.kehutong.admin.service.CorpDatabaseService;
import com.kehutong.auth.security.AuthorizingRealm;
import com.kehutong.charge.util.ChargeOrderInterimJob;
import com.kehutong.charge.util.ChargeOrderJob;
import com.kehutong.charge.util.ChargePenaltyJob;
import com.kehutong.charge.util.ChargeUrgingJob;
import com.kehutong.common.BaseLauncher;
import com.kehutong.common.schedule.QuartzManager;
import com.kehutong.crm.worker.DemandForecastWorker;
import com.kehutong.scheduled.util.ScheduledTasker;

@BootApplication
//@BootApplication(isCreateTable = false, isModifyTable = false)
public class Launcher extends BaseLauncher  implements DBInit {

	static final Logger logger = LoggerFactory.getLogger(Launcher.class);

	public static void main(String[] args) throws IOException {
		Application.run(Launcher.class);
	}

	public static void run() {
		Application.run(Launcher.class);
	}

	@Binding
	public void bind(Binder binder) {
		super.bind(binder);
		binder.bind(AuthzRealm.class).to(AuthorizingRealm.class);
	}

	@Initialize
	public void init(ServletContextEvent Event) throws Exception {
		super.init(Event);

		ScheduledTasker tasker = Ioc.getInstance(ScheduledTasker.class);
//		tasker.addWork(new GenerateBillWorker());
//		tasker.addWork(new ChargeCallTaskWorker());

		QuartzManager.addJob("demandForecast", null, new DemandForecastWorker(), "0 0 1 * * ?");// "0 0 1 * * ?" 0 */5 * * * ?

		/**
		 * 缴费定时任务
		 */
		QuartzManager.addJob("chargeOrderJob", null, new ChargeOrderJob(), "0 0/1 * * * ?"); //默认每分钟执行一次 常规缴费订单超过
		QuartzManager.addJob("chargeOrderInterimJob", null, new ChargeOrderInterimJob(), "0 0/1 * * * ?");//默认每分钟执行一次 临时规缴费订单超过
		QuartzManager.addJob("chargePenaltyJob", null, new ChargePenaltyJob(), "0 0/10 * * * ?");//默认每天0点执行一次 违约金

		/** 催费定时任务 */
		QuartzManager.addJob("chargeUrgingJob", null, new ChargeUrgingJob(), "0 0 9 ? * *");// 0 0 9 ? * * 每天9:00执行催缴
		// QuartzManager.addJob("chargeUrgingJob", null, new ChargeUrgingJob(), "0 0/10 * * * ?");// 0 10 9 ? * * 每天9:10执行催缴

		QuartzManager.addJob("chargeBillHouseJob", null, new ChargeBillHouseJob(), "1 0 0 * * ?");// "0 0 1 * * ?" 0 */5 * * * ?
	}

	@Override
	public IDBConfig getMainDB() {
		return new DataBaseConfig(Application.getConfig("mainDB"));
	}

	@Override
	public IDBConfig[] getCorpDB() {
		CorpDatabaseService corpDb = Ioc.getInstance(CorpDatabaseService.class);

		IDBConfig[] configList = corpDb.getDBList().toArray(new IDBConfig[0]);
		return configList;
	}

}